﻿using mediaControl.Event;
using mediaControl.Vo;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace mediaControl.ViewModels
{
    public class Mp4PlayerViewModel : BindableBase
    {
        //事件聚合器
        private readonly IEventAggregator _eventAggregator;

        private bool isPause = false;

        private ProgramItemVo currentProgram;

        private string cycleModel;

        private List<ProgramItem> currentProgramList;

        private int index = 0;

        private MediaElement videoPlayer;

        //缩放
        private String stretch;

        private ScreenVo screenVo;

        public Window CurrentWindow { get; set; }

        public  DelegateCommand<Object> MediaEndedCommand { get; set; }

        public Mp4PlayerViewModel(DialogParameters parameters, IEventAggregator eventAggregator)
        {
            currentProgramList = new List<ProgramItem>();
            currentProgram = parameters.GetValue<ProgramItemVo>("programItem");
            List<PlaySourceVo> playSourceVos = parameters.GetValue<List<PlaySourceVo>>("source");
            screenVo = parameters.GetValue<ScreenVo>("screen");
            cycleModel = parameters.GetValue<string>("cycleModel");
            foreach (PlaySourceVo playSourceVo in playSourceVos) {
                currentProgramList.Add(playSourceVo.SouceItem);
            }
            foreach (Window win in Application.Current.Windows)
            {
                MediaElement video = win.FindName("videoPlayer") as MediaElement;
                if (video != null)
                {
                    videoPlayer = video;
                }
            }
            Volume = String.IsNullOrEmpty(currentProgram.Volume) ? 1 : Convert.ToDouble(currentProgram.Volume)/100;
            if (currentProgramList[0].IsMute) {
                Volume = 0;
            }
            CurrentWindow = parameters.GetValue<Window>("win");
            if (String.IsNullOrEmpty(currentProgramList[0].DisplayMode) || currentProgramList[0].DisplayMode.Equals("1")) {
                //全屏
                CurrentWindow.WindowState = WindowState.Maximized;
                CurrentWindow.ResizeMode = ResizeMode.NoResize;
                CurrentWindow.WindowStyle = WindowStyle.None;
                if (null != videoPlayer) {
                    videoPlayer.Width = screenVo.Width;
                    videoPlayer.Height=screenVo.Height;
                    videoPlayer.Stretch = System.Windows.Media.Stretch.Fill;
                }
                
            }
            if (!String.IsNullOrEmpty(currentProgramList[0].DisplayMode) && currentProgramList[0].DisplayMode.Equals("2")) {
                //视屏自身生比例
                videoPlayer.Width = screenVo.Width;
                videoPlayer.Height = screenVo.Height;
                videoPlayer.Stretch = System.Windows.Media.Stretch.None;
            }
            if (!String.IsNullOrEmpty(currentProgramList[0].DisplayMode) && currentProgramList[0].DisplayMode.Equals("3"))
            {
                //16:9
                videoPlayer.Width = screenVo.Width;
                videoPlayer.Height = screenVo.Height/16*9;
                videoPlayer.Stretch = System.Windows.Media.Stretch.Fill;
            }
            if (!String.IsNullOrEmpty(currentProgramList[0].DisplayMode) && currentProgramList[0].DisplayMode.Equals("4"))
            {
                //4:3
                videoPlayer.Width = screenVo.Width;
                videoPlayer.Height = screenVo.Width/4*3;
                videoPlayer.Stretch = System.Windows.Media.Stretch.Fill;
            }
            VideoSrc = currentProgramList[0].ItemPath;
            //StartWith = String.IsNullOrEmpty(programItemVo.StartWith) ? 0 : Convert.ToInt16(programItemVo.StartWith);

            MediaEndedCommand = new DelegateCommand<object>(mediaEnded);
            _eventAggregator = eventAggregator;
            eventAggregator.GetEvent<PlayStatusEvent>().Subscribe(pause);
        }

        private void mediaEnded(object obj)
        {
            videoPlayer = obj as MediaElement;
            videoPlayer.Stop();
            switch (cycleModel) {
                case "1":
                    index++;
                    if (index < currentProgramList.Count) {
                        videoPlayer.Source = new Uri(currentProgramList[index].ItemPath);
                        if (currentProgramList[index].IsMute)
                        {
                            Volume = 0;
                        }
                        videoPlayer.Play();
                        if (String.IsNullOrEmpty(currentProgramList[index].DisplayMode) || currentProgramList[0].DisplayMode.Equals("1"))
                        {
                            //全屏

                            if (null != videoPlayer)
                            {
                                videoPlayer.Width = screenVo.Width;
                                videoPlayer.Height = screenVo.Height;
                                videoPlayer.Stretch = System.Windows.Media.Stretch.Fill;
                            }

                        }
                        if (!String.IsNullOrEmpty(currentProgramList[index].DisplayMode) && currentProgramList[0].DisplayMode.Equals("2"))
                        {
                            //视屏自身生比例
                            videoPlayer.Width = screenVo.Width;
                            videoPlayer.Height = screenVo.Height;
                            videoPlayer.Stretch = System.Windows.Media.Stretch.None;
                        }
                        if (!String.IsNullOrEmpty(currentProgramList[index].DisplayMode) && currentProgramList[0].DisplayMode.Equals("3"))
                        {
                            //16:9
                            videoPlayer.Width = screenVo.Width;
                            videoPlayer.Height = screenVo.Height / 16 * 9;
                            videoPlayer.Stretch = System.Windows.Media.Stretch.UniformToFill;
                        }
                        if (!String.IsNullOrEmpty(currentProgramList[index].DisplayMode) && currentProgramList[0].DisplayMode.Equals("4"))
                        {
                            //4:3
                            videoPlayer.Width = screenVo.Width;
                            videoPlayer.Height = screenVo.Width / 4 * 3;
                            videoPlayer.Stretch = System.Windows.Media.Stretch.UniformToFill;
                        }
                    }
                    break;
                default:
                    index++;
                    if (index >= currentProgramList.Count)
                    {
                        index = 0;
                    }
                    videoPlayer.Source = new Uri(currentProgramList[index].ItemPath);
                    if (currentProgramList[index].IsMute)
                    {
                        Volume = 0;
                    }
                    if (String.IsNullOrEmpty(currentProgramList[index].DisplayMode) || currentProgramList[0].DisplayMode.Equals("1"))
                    {
                        //全屏

                        if (null != videoPlayer)
                        {
                            videoPlayer.Width = screenVo.Width;
                            videoPlayer.Height = screenVo.Height;
                            videoPlayer.Stretch = System.Windows.Media.Stretch.Fill;
                        }

                    }
                    if (!String.IsNullOrEmpty(currentProgramList[index].DisplayMode) && currentProgramList[0].DisplayMode.Equals("2"))
                    {
                        //视屏自身生比例
                        videoPlayer.Width = screenVo.Width;
                        videoPlayer.Height = screenVo.Height;
                        videoPlayer.Stretch = System.Windows.Media.Stretch.None;
                    }
                    if (!String.IsNullOrEmpty(currentProgramList[index].DisplayMode) && currentProgramList[0].DisplayMode.Equals("3"))
                    {
                        //16:9
                        videoPlayer.Width = screenVo.Width;
                        videoPlayer.Height = screenVo.Height / 16 * 9;
                        videoPlayer.Stretch = System.Windows.Media.Stretch.UniformToFill;
                    }
                    if (!String.IsNullOrEmpty(currentProgramList[index].DisplayMode) && currentProgramList[0].DisplayMode.Equals("4"))
                    {
                        //4:3
                        videoPlayer.Width = screenVo.Width;
                        videoPlayer.Height = screenVo.Width / 4 * 3;
                        videoPlayer.Stretch = System.Windows.Media.Stretch.UniformToFill;
                    }
                    videoPlayer.Play();
                    break;
            }
        }

        private void pause(string obj)
        {
            isPause = true;
        }

        private void play(ProgramItemGroupVo obj)
        {
            currentProgram = obj.CurrentProgram;
            //
        }

        private string videoSrc;

        private double volume;
        public string VideoSrc { get { return videoSrc; } set { this.videoSrc = value; RaisePropertyChanged(); } }

        public double Volume { get { return volume; } set { volume = value; RaisePropertyChanged(); } }

        public string Stretch { get => stretch; set => stretch = value; }
    }
}
